package com.wb.web.controller.position;

import com.wb.common.annotation.Anonymous;
import com.wb.common.core.controller.BaseController;
import com.wb.common.core.domain.AjaxResult;
import com.wb.common.core.page.TableDataInfo;
import com.wb.common.exception.base.BaseException;
import com.wb.common.utils.StringUtils;
import com.wb.common.vo.PositionModelVO;
import com.wb.system.domain.PositionModel;
import com.wb.system.service.IPositionService;
import com.wb.web.controller.position.vo.PositionModelCreateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

/**
 *
 *
 * @Description TODO
 * @Author zhangxl
 * @Date 2025/7/10 15:23
 */
@RestController
@RequestMapping("position")
@Api(tags = "职位")
public class PositionController extends BaseController {
    private String prefix = "position";
    @Autowired
    private IPositionService positionService;

    @GetMapping("/list")
    @ApiOperation("岗位列表")
    public TableDataInfo list(PositionModelVO positionModelVO) {
        startPage();
        //LambdaQueryWrapper<PositionModel> positionModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //positionModelLambdaQueryWrapper.like(!StringUtils.isEmpty(positionModelVO.getKeyword()),PositionModel::getDescription,positionModelVO.getKeyword());
        //positionModelLambdaQueryWrapper.like(!StringUtils.isEmpty(positionModelVO.getLocation()),PositionModel::getLocation,positionModelVO.getLocation());
        //positionModelLambdaQueryWrapper.like(!StringUtils.isEmpty(positionModelVO.getCompany()),PositionModel::getCompany,positionModelVO.getCompany());
        List<PositionModel> list = positionService.selectPositionList(positionModelVO);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(positionModel -> {
                String requirement = positionModel.getRequirements();
                if (StringUtils.isNotEmpty(requirement)) {
                    positionModel.setRequirementList(Arrays.asList(requirement.split(",")));
                }
                String benefits = positionModel.getBenefits();
                if (StringUtils.isNotEmpty(benefits)) {
                    positionModel.setBenefitList(Arrays.asList(benefits.split(",")));
                }
            });
        }
        return getDataTable(list);
    }

    @PostMapping("/add")
    @ApiOperation("职位创建")
    public AjaxResult add(@Valid @RequestBody PositionModelCreateVO position){
        PositionModel positionModel = new PositionModel();
        BeanUtils.copyProperties(position,positionModel);
        if (!CollectionUtils.isEmpty(position.getRequirements())) {
            positionModel.setRequirements(StringUtils.join(position.getRequirements(), ","));
        }
        if (!CollectionUtils.isEmpty(position.getBenefits())) {
            positionModel.setBenefits(StringUtils.join(position.getBenefits(), ","));
        }
        positionService.savePosition(positionModel);
        return AjaxResult.success();
    }

    @GetMapping("/getPosition")
    @ApiOperation("职位详情")
    @Anonymous
    public AjaxResult getPosition(Long positionId){
        PositionModel positionModel = positionService.getPositionDetail(positionId);
        String requirement = positionModel.getRequirements();
        if (StringUtils.isNotEmpty(requirement)) {
            positionModel.setRequirementList(Arrays.asList(requirement.split(",")));
        }
        String benefits = positionModel.getBenefits();
        if (StringUtils.isNotEmpty(benefits)) {
            positionModel.setBenefitList(Arrays.asList(benefits.split(",")));
        }
       // positionModel.setCreateBy(getUsername());
        return AjaxResult.success(positionModel);
    }

    @PostMapping("/update")
    @ApiOperation("更新职位")
    public AjaxResult update(@RequestBody PositionModelCreateVO position){
        PositionModel positionModel = new PositionModel();
        BeanUtils.copyProperties(position,positionModel);
        if (!CollectionUtils.isEmpty(position.getRequirements())) {
            positionModel.setRequirements(StringUtils.join(position.getRequirements(), ","));
        }
        if (!CollectionUtils.isEmpty(position.getBenefits())) {
            positionModel.setBenefits(StringUtils.join(position.getBenefits(), ","));
        }
        positionModel.setUpdateBy(getUsername());
        positionService.updatePositionById(positionModel);
        return AjaxResult.success();
    }

    @DeleteMapping("/batchDelete")
    @ApiOperation("删除职位")
    public AjaxResult batchDelete(List<Long> positionIds){
        positionService.batchDeletePosition(positionIds);
        return AjaxResult.success();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除职位")
    public AjaxResult delete(Long positionId){
        positionService.removePositionById(positionId);
        return AjaxResult.success();
    }
    @DeleteMapping("/deleteAll")
    @ApiOperation("删除职位All")
    public AjaxResult deleteAll(@RequestParam("ids") List<Long> positionIds){
        positionService.removePositionByIds(positionIds);
        return AjaxResult.success();
    }
    @PostMapping("/updateStatus")
    @ApiOperation("更新岗位状态")
    public AjaxResult updateStatus(@RequestBody PositionModel positionModel){
        if (null == positionModel || StringUtils.isNull(positionModel.getPositionId())){
            throw new BaseException("参数对象或ID为空，更新职位状态失败");
        }
        PositionModel entity = new PositionModel();
        entity.setPositionId(positionModel.getPositionId());
        entity.setStatus(positionModel.getStatus());
        positionService.updateStatus(entity);
        return AjaxResult.success();
    }



}
